package homework;

/**
 * @author wly
 * @create 2022-12-10 23:22
 */
public class Homework06 {
    public static void main(String[] args) {
        Person p = new Person("river", new Horse());
        p.common();
        p.passRiver();
    }
}

interface Vehicles{
    void work();
}

class Horse implements Vehicles{

    @Override
    public void work() {
        System.out.println("我骑着马，你挑着担");
    }
}

class Boat implements Vehicles{

    @Override
    public void work() {
        System.out.println("泰坦尼克号沉船");
    }
}

class VehicleFactory{
    private static Horse horse = new Horse();
    public static Horse getHorse(){
        return horse;
    }

    public static Boat getBoat(){
        return new Boat();
    }
}

class Person{
    private Vehicles vehicles;
    private String name;
    public Person(String name, Vehicles vehicles){
        this.name = name;
        this.vehicles = vehicles;
    }

    public void common(){
//        判断当前vehicles 属性已经存在
        if (vehicles instanceof Boat){
            vehicles = VehicleFactory.getHorse();
        }
//        使用接口调用 多态
        vehicles.work();
    }

    public void passRiver(){
        if (vehicles instanceof Horse){
            vehicles = VehicleFactory.getBoat();
        }
        vehicles.work();
    }
}
